package myReader;

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Properties;
import javax.mail.*;

public class ReadMail {
	
	public static void main(String args[])
	{
	String username = "mpc.dummy";
	String password = "sensing3D"; 

	try{
		Properties p = new Properties();
		Session session = Session.getDefaultInstance(p, null);
		Store store = session.getStore("pop3s");
		try {
			store.connect("pop.gmail.com",995,username,password);
			Folder folder = store.getFolder("INBOX");
			folder.open(Folder.READ_ONLY);
			Message message[] = folder.getMessages(); 
			PrintWriter out = new PrintWriter(new FileWriter("Inbox.txt"));
			for(int i=0,n=message.length;i<n;i++)
			{
				String mails_list = i+":"+message[i].getFrom()[0]+"\t"+message[i].getSubject();
				System.out.println(mails_list);
				out.println(mails_list);
				Runtime.getRuntime().exec("notepad Inbox.txt");
			}
			out.close();
			folder.close(false); 
			store.close();
		} catch (MessagingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	catch(NoSuchProviderException e)
	{
		e.printStackTrace();
	}	
  }
	
}